﻿using System;
using System.Windows.Forms;
using NUnit.Framework;
using Nuwanda.Common;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;
using Nuwanda.TfsSpotlight.Shell.Services;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;
using Rhino.Mocks;

namespace TfsSpotlight.Tests.Core.Services
{
    [TestFixture]
    [Author("Bartłomiej Legiędź")]
    public class TabsServiceTests
    {
        private MockRepository _mocks;
        private ITabStrip _tabStripMock;
        private IAsyncService _asyncServiceMock;

        private ITabsService _tabsServiceUnderTest;

        [SetUp]
        public void SetUp()
        {
            this._mocks = new MockRepository();
            this._tabStripMock = this._mocks.StrictMock<ITabStrip>();
            this._asyncServiceMock = this._mocks.StrictMock<IAsyncService>();

            this._tabsServiceUnderTest = new TabsService(this._tabStripMock, this._asyncServiceMock);
        }

        [Test]
        public void WhenAskedTo_OpenTab_And_ThisTabIsntAlreadyOpened_Should_OpenNewTab_And_SetItToActive()
        {
            var presenter = this._mocks.StrictMock<ITabPresenter>();
            ITabStripItem createdTabItem = null;
            var controlToReturn = new Control();

            using(this._mocks.Record())
            {
                Expect.Call(presenter.Control).Return(controlToReturn);

                presenter.Tab = null;
                LastCall.IgnoreArguments()
                    .Do((Action<ITabStripItem>)delegate(ITabStripItem tab)
                    {
                        createdTabItem = tab;
                    });

                this._tabStripMock.AddTabAndActivate(null);
                LastCall.IgnoreArguments()
                    .Do((Action<ITabStripItem>)(tab => Assert.AreSame(createdTabItem, tab)));
                this._asyncServiceMock.QueueInPool(null);
                LastCall.IgnoreArguments();
            }

            using(this._mocks.Playback())
            {
                this._tabsServiceUnderTest.OpenTab(presenter);

                Assert.AreSame(presenter, createdTabItem.Presenter);
                Assert.AreSame(controlToReturn, createdTabItem.View);   
            }
        }
    }
}